package edu.hrbeu.crc.pojo.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

@Data
public class Query implements Serializable {

    // 序列化版本号
    @Serial
    private static final long serialVersionUID = 1L;

    // 页数
    private Integer pageNo;
    // 页码
    private Integer pageSize;
    // 排序字段
    private String sortBy;
    // 是否升序
    private Boolean isAsc;

    public <V> Page<V> toPage(OrderItem ...orders) {
        // 1.页数、页码
        Page<V> p = Page.of(pageNo,pageSize);
        // 2.排序
        if (sortBy != null) {
            p.addOrder(isAsc ? OrderItem.asc(sortBy): OrderItem.desc(sortBy));
        }
        // orders?
        if (orders != null) {
            p.addOrder(orders);
        }
        return p;
    }

    public <V> Page<V> toPage(String defaultSortBy, Boolean isAsc) {
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(defaultSortBy);
        orderItem.setAsc(isAsc);
        return this.toPage(orderItem);
    }

    public <V> Page<V> toDefaultSortByCreateTimeDesc() {
        return this.toPage("create_time",false);
    }
}
